//
//  NIDatePickerView.m
//  gweAD
//
//  Created by navinfo air on 2017/2/21.
//  Copyright © 2017年 mengy. All rights reserved.
//

#import "NIDatePickerView.h"

@implementation NIDatePickerView
{
    
    __weak IBOutlet UIView *contentView;
    __weak IBOutlet UIDatePicker *datePicker;
    NSString *selectDate;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
}

-(void)setCenterDate:(NSString *)dateStr
{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //设置时间格式
    formatter.dateFormat = @"yyyy年MM月dd日";
    NSDate *date = [formatter dateFromString:dateStr];
    datePicker.date = date;
}

- (void)show{
    self.hidden = NO;
    
}

- (void)close{
    self.hidden = YES;
}

#pragma mark - UIPickerView
- (IBAction)onCloseButtonOfPickViewClick:(id)sender{
    [self close];
}
- (IBAction)onClickCancel:(id)sender {
    [self close];
}

- (IBAction)onDoneButtonClick:(id)sender{
    [self close];
    if (self.delegate) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        //设置时间格式
        formatter.dateFormat = @"yyyy年MM月dd日";
        selectDate = [formatter  stringFromDate:datePicker.date];

        [self.delegate dateDidSelected:selectDate];
    }
}



@end
